#include <iostream>
using namespace std;

const int N = 1e5 + 10;
int q[N], h, t, n;
void push(int x)
{
    q[++t] = x;
}

void pop()
{
    h++;
}

int query()
{
    return q[h + 1];
}

int size()
{
    return t - h;
}

bool empty()
{
    return h == t;
}
int main()
{
    cin >> n;
    while(n--)
    {
        int op;
        cin >> op;
        if(op == 1)
        {
            // push
            int x;
            cin >> x;
            push(x);
        }
        else if(op == 2)
        {
            if(empty()) cout << "ERR_CANNOT_POP" << endl;
            else pop();
        }
        else if(op == 3)
        {
            if(empty()) cout << "ERR_CANNOT_QUERY" << endl;
            else cout << query() << endl;
        }
        else
        {
            cout << size() << endl;
        }
    }
    return 0;
}